package com.ndhutu.ee.util.dwz.baidu;

import com.alibaba.fastjson.JSONObject;
import com.ndhutu.ee.util.dwz.DwzConfig;
import com.ndhutu.ee.util.http.OKHttpUtil;
import okhttp3.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author AppiJ
 * @Date 2020/4/20 14:11
 */
public class DwzUtil {
    final static String CREATE_API = "https://dwz.cn/admin/v2/create";
    final static String TERMOFVALIDITY = "1-year";

    /**
     * 创建短网址
     * @Author AppiJ
     * @Date 2020/4/20 14:12
     * @param
     * @return
     */
    public static String createShortUrl(String longUrl, DwzConfig config) throws Exception{

        String params = "{\"Url\":\""+ longUrl + "\",\"TermOfValidity\":\""+ TERMOFVALIDITY + "\"}";

        Map<String, String> token = new HashMap<String, String>();
        token.put("Token", config.getToken());

        String resultJson = OKHttpUtil.post(CREATE_API, params, token);

        Map<String, Object> map = JSONObject.parseObject(resultJson, Map.class);
        if(Integer.parseInt(map.get("Code").toString()) == 0){
            return map.get("ShortUrl").toString();
        }
        return null;
    }

    public static String getShortUrls(String token, String longurl) throws Exception{
        String url = "https://dwz.cn/api/v3/short-urls";
        Map<String, String> headers = new HashMap<>();
        headers.put("Dwz-Token", token);
        headers.put("Content-Type", "application/json; charset=UTF-8");

        Map<String, String> params = new HashMap<>();
        params.put("LongUrl", longurl);
        params.put("TermOfValidity", "1-year");
        List<Map> lists = new ArrayList<>();
        lists.add(params);


        RequestBody body = RequestBody.create( MediaType.parse("application/json; charset=utf-8"), JSONObject.toJSONString(lists));
        Request.Builder builder = new Request.Builder();
        for(Map.Entry<String, String> entry : headers.entrySet()){
            builder.addHeader(entry.getKey(), entry.getValue());
        }
        builder = builder.url(url).post(body);

        Request request = builder.build();
        Call call = new OkHttpClient().newCall(request);
        return call.execute().body().string();

    }

    public static void main(String[] args) throws Exception{
        /*try {
            System.out.println(createShortUrl("http://share.mm.jvtsydn.cn/invite/index.html?channelCode=&inviteCode=ST1925", new DwzConfig() {
                public String getToken() {
                    return "0a667a492d019ce7bbcbf148bff5c32c";
                }
                public String getKey() {
                    return null;
                }
            }));
        }catch (Exception e){
            e.printStackTrace();
        }*/

        String token = "0a667a492d019ce7bbcbf148bff5c32c";
        String url = "http://share.mm.ppmfred.com/invite";

        String result = getShortUrls(token, url);
        System.out.println(result);
    }
}
